package com.bjqh.edu.Base;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bjqh.edu.Bean.Course;
import com.bjqh.edu.R;
import com.bumptech.glide.Glide;

import java.text.DecimalFormat;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/9/21.
 */

public class OrderPopupWindow extends PopupWindow {


    private Button btn_payment;
    private TextView tv_des,tvName,tvPp,tvTotal,tvCount;
    private ImageView ivImg;
    private View mMenuView;

    public OrderPopupWindow(Activity context, View.OnClickListener itemsOnClick, Course.RowsBean rowsBean) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.order_dialog, null);
        btn_payment = (Button) mMenuView.findViewById(R.id.btn_payment);
        tv_des = (TextView) mMenuView.findViewById(R.id.tv_des);
        tvName = (TextView) mMenuView.findViewById(R.id.tv_name);
        tvPp = (TextView) mMenuView.findViewById(R.id.tv_pp);
        tvCount = (TextView) mMenuView.findViewById(R.id.tv_count);
        tvTotal = (TextView) mMenuView.findViewById(R.id.tv_total);
        ivImg = (ImageView) mMenuView.findViewById(R.id.iv_img);
        Double op = rowsBean.getPrice();
        tv_des.setText(rowsBean.getDes());
        tvName.setText("<"+rowsBean.getName2()+">X1");
        Float count = Float.parseFloat(rowsBean.getDiscount());
        Double pp = count / 10 * op;
        DecimalFormat df = new DecimalFormat("#0.00");
        tvPp.setText("￥" + String.valueOf(df.format(pp)));
        tvCount.setText("￥" + String.valueOf(df.format(pp)));
        tvTotal.setText("￥" + String.valueOf(df.format(pp)));
        Glide.with(context)
                .load(AppConstants.IMG_URL+rowsBean.getImg())
                .asBitmap()
                .placeholder(R.mipmap.kcgm_bj)
                .into(ivImg);
        //设置按钮监听
        btn_payment.setOnClickListener(itemsOnClick);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

}